<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'detail';
if($_RQ['op']=='detail'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择医生','error','jy_zhongyi/index/');
	}
	$this->session('id',$id);
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$id);
	if($_RQ['keyword']){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
	}
	$item=pdo_get('jy_zhongyi_worker',$params);
	$label=str_array($item['label']);
	$item['label']=array();
	foreach($label as $lv){
		$getlabel=pdo_get('jy_zhongyi_label',array('id'=>$lv,'uniacid'=>$_SESSION['uniacid']));
		if($getlabel){
			$item['label'][]=$getlabel;
		}
	}
	$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
	$item['lessontitle']=pdo_getcolumn('jy_zhongyi_lesson',array('id'=>$item['lesson_id']),'title');
	$item['storetitle']=pdo_getcolumn('jy_zhongyi_store',array('id'=>$item['storeid']),'title');
	$item['downnum']=pdo_count('jy_zhongyi_lesson_log',array('worker_id'=>$item['id']),'id');
	$item['allnum']=pdo_count('jy_zhongyi_lesson_log',array('worker_id'=>$item['id']),'id');
	$item['pingjianum']=pdo_count('jy_zhongyi_pingjia',array('user_id'=>$item['id']),'id');
	$return['item']=$item;
	$return['ewmurl']=custom_url('jy_zhongyi/worker_item//id='.$id,true);
	$return['sharedata']=array(
		'title'=>$item['title']?$item['title']:'大夫详情',
		'desc'=>$item['desc']?$item['desc']:'大夫说明',
		'link'=>custom_url('jy_zhongyi/worker_item//id='.$id,true),
		'imgUrl'=>tomedia($item['thumb']),
	);
	$months=$this->model('lesson')->get_months($item['id'],$item['storeid']);
	$return['months']=$months;
	$like=pdo_get('jy_zhongyi_worker_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'worker_id'=>$id),'id');
	if($like){
		$return['islike']=1;
	}
	set_html('title',$item['title']);
	$return['type']=0;
	exi($return);
}
if($_RQ['op']=='getpingjia'){
	$post=get_postdata();
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	$type=intval($_RQ['type']);
	if($_RQ['user_id']){
		$params['user_id']=intval($_RQ['user_id']);
	}
	//排序医生
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$return['status']=0;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'fields'=>$fields,
		'orderby'=>$orderby,
	);
	$list=pdo_getslice('jy_zhongyi_pingjia',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$label=str_array($item['label']);
			$item['label']=array();
			foreach($label as $lv){
				$item['label'][]=pdo_get('jy_zhongyi_pjlabel',array('id'=>$lv,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='like'){
	if(!$_SESSION['uid']){
		$this->model('member')->checklogin();
	}
	$id = $this->session('id');
	if(!$id){
		$post=get_postdata();
	}
	$like=pdo_get('jy_zhongyi_worker_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'worker_id'=>$id),'id');
	if($like){
			pdo_delete('jy_zhongyi_worker_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'worker_id'=>$id));
			exi('取消成功');
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'worker_id'=>$id,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_zhongyi_worker_like',$insert);
		exi('关注成功');
	}	
}
// 完成下单
if($_RQ['op']=='linebuy'){
	$workerid=$_RQ['workerid'];
	if(!$workerid){
		exi('请选择大夫！','error');
	}
	$new_order=array(
		'mid'=>$_SESSION['uid'],
		'worker_id'=>$workerid,
		'day'=>date('Y-m-d'),
		'createtime'=>time(),
	);
	$new_order['fee']=pdo_getcolumn('jy_zhongyi_worker',array('id'=>$new_order['worker_id']),'lineprice');
	if($new_order['fee']<=0){
		$new_order['ispay']=1;
	}
	$logid=model('jy_zhongyi/online')->add_feelog($new_order);
	if($new_order['fee']>0){
		$params=array(
			'id'=>$logid,
			'title'=>'在线问诊服务',
			'fee'=>$new_order['fee'],
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			//'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_zhongyi/worker_item/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>'jy_zhongyi/online.log//',
			'error_url'=>'jy_zhongyi/online.log//',
		);
		$payid=cfc('pay')->create_pay_order($params);
		pdo_update('jy_zhongyi_linelog',array('pay_id'=>$payid),array('id'=>$logid));
		$res=array(
			'pay_id'=>$payid,
			'order_id'=>$logid,
		);
		exi($res);
	}else{
		exi('购买在线问诊服务成功！','','/online.log/');
	}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_zhongyi/online.log/');
	}
	$order=pdo_get('jy_zhongyi_linelog',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_zhongyi/online.log/');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['fee'].'|'.$payresult['fee'],'error','jy_zhongyi/online.log/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
	);
	$url=custom_url('jy_zhongyi/online.log//',true);
	model('jy_zhongyi/online')->order_pay($order['id'],$params,$url);
	exi('支付成功!');
}
?>